//
//  CRTopicCell.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/27.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRTopicCell.h"
#import "CRTopicPictureView.h"
#import "CRTopicVideoView.h"
#import "CRTopicVoiceView.h"
#import "UIImageView+CRExtension.h"
#import "CRTopic.h"
#import "CRUser_MK.h"
#import "CRComment.h"
#import "MBProgressHUD+MJ.h"
@interface CRTopicCell ()
@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *createTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_label;

@property (weak, nonatomic) IBOutlet UIButton *dingButton;
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
@property (weak, nonatomic) IBOutlet UIButton *repostButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
/** 最热评论label */
@property (weak, nonatomic) IBOutlet UILabel *topCmtLabel;
/** 最热评论整体 */
@property (weak, nonatomic) IBOutlet UIView *topCmtView;

/** 图片控件 */
@property (nonatomic, weak) CRTopicPictureView *pictiureView;

/** 声音控件 */
@property (nonatomic, weak) CRTopicVoiceView *voiceView;
/** 视频控件 */
@property (nonatomic, weak) CRTopicVideoView *videoView;
@end

@implementation CRTopicCell
#pragma mark >>>> 懒加载
- (CRTopicPictureView *)pictiureView {
    if (!_pictiureView) {
        CRTopicPictureView *pictiureView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CRTopicPictureView class]) owner:nil options:nil] lastObject];
        [self.contentView addSubview:pictiureView];
        _pictiureView = pictiureView;
    }
    return _pictiureView;
}

- (CRTopicVoiceView *)voiceView {
    if (!_voiceView) {
        CRTopicVoiceView *voiceView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CRTopicVoiceView class]) owner:nil options:nil] lastObject];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
    return _voiceView;
}

- (CRTopicVideoView *)videoView {
    if (!_videoView) {
        CRTopicVideoView *videoView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CRTopicVideoView class]) owner:nil options:nil] lastObject];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}

#pragma mark >>>> 初始化
- (void)awakeFromNib {
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
}

- (void)setTopic:(CRTopic *)topic {
    if (_topic != topic) {
        _topic = nil;
        _topic = topic;
        [self setupSubviews];
    }
}


- (void)setupSubviews {
    if (!self.topic.isDing) {
        self.dingButton.enabled = YES;
    }
    
    if (!self.topic.isCai) {
        self.caiButton.enabled = YES;
    }
    
    // 顶部数据
    [self.profileImageView setHeaderWithUrl:self.topic.profile_image];
    self.nameLabel.text = self.topic.name;
    self.text_label.text = self.topic.text;
    self.createTimeLabel.text = self.topic.created_at;

    
    // 底部工具条
    [self setupToolbarButtonText:self.dingButton count:self.topic.ding title:@"赞"];
    [self.dingButton setImage:[UIImage imageNamed:@"mainCellDingClick"] forState:UIControlStateDisabled];
    [self.dingButton addTarget:self action:@selector(clickDingButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self setupToolbarButtonText:self.caiButton count:self.topic.cai title:@"踩"];
    [self.caiButton setImage:[UIImage imageNamed:@"mainCellCaiClick"] forState:UIControlStateDisabled];
    [self.caiButton addTarget:self action:@selector(clickCaiButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self setupToolbarButtonText:self.repostButton count:self.topic.repost title:@"分享"];
    [self.repostButton addTarget:self action:@selector(clickRepostButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self setupToolbarButtonText:self.commentButton count:self.topic.comment title:@"评论"];
    [self.commentButton addTarget:self action:@selector(clickCommentButton:) forControlEvents:UIControlEventTouchUpInside];
    
    // 最热评论
    if (self.topic.top_cmt) {
        self.topCmtView.hidden = NO;
        self.topCmtLabel.text = [NSString stringWithFormat:@"%@ : %@", self.topic.top_cmt.user, self.topic.top_cmt.content];
    } else {
        self.topCmtView.hidden = YES;
    }
    
    // 中间的具体内容
    switch (self.topic.type) {
        case CRTopicTypeWord: { // 文字
            self.text_label.numberOfLines = 0;
            self.pictiureView.hidden = YES;
            self.voiceView.hidden = YES;
            self.videoView.hidden = YES;
            break;
        }
        case CRTopicTypePicture: {
            self.pictiureView.hidden = NO;
            self.pictiureView.frame = self.topic.contentFrame;
            self.pictiureView.topic = self.topic;
            self.videoView.hidden = YES;
            self.voiceView.hidden = YES;
            break;
        }
        case CRTopicTypeVoice: {
            self.pictiureView.hidden = YES;
            self.voiceView.hidden = NO;
            self.voiceView.frame = self.topic.contentFrame;
            self.voiceView.topic = self.topic;
            self.videoView.hidden = YES;
            break;
        }
        case CRTopicTypevideo: {
            self.pictiureView.hidden = YES;
            self.voiceView.hidden = YES;
            self.videoView.hidden = NO;
            self.videoView.frame = self.topic.contentFrame;
            self.videoView.topic = self.topic;
            break;
        }
        default:
            break;
    }
}
- (IBAction)didClickMoreButton:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"点击了收藏按钮");
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"点击了举报按钮");
    }]];
    
    [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
    
}

- (void)setFrame:(CGRect)frame {
    frame.size.height -= CRMargin;
    frame.origin.y += CRMargin;
    
    [super setFrame:frame];
}

#pragma mark >>>> 监听事件
- (void)clickDingButton:(UIButton *)button {
    self.topic.isDing = YES;
    button.enabled = NO;
    
    NSInteger num = self.topic.ding + 1;
    [self.dingButton setTitle:[NSString stringWithFormat:@"%ld", num] forState:UIControlStateDisabled];
    [self.dingButton setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled];
    [self setNeedsDisplay];
}

- (void)clickCaiButton:(UIButton *)button {
    self.topic.isCai = YES;
    button.enabled = NO;
    NSInteger num = self.topic.cai + 1;
    [self.caiButton setTitle:[NSString stringWithFormat:@"%ld", num] forState:UIControlStateDisabled];
    [self.caiButton setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled];
    [self setNeedsDisplay];
}

- (void)clickRepostButton:(UIButton *)button {
    [UIView animateWithDuration:1.5 animations:^{
        [MBProgressHUD showMessage:@"敬请期待....."];
    } completion:^(BOOL finished) {
        [MBProgressHUD hideHUD];
    }];
}
- (void)clickCommentButton:(UIButton *)button {
    
}
#pragma mark >>>> 其他方法
/**
 *  设置工具条按钮的文字
 *
 *  @param button 按钮
 *  @param count  数量
 *  @param title  数量为0时显示的文字
 */
- (void)setupToolbarButtonText:(UIButton *)button count:(NSInteger)count title:(NSString *)title {
   
    if (count >= 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万", count / 10000.0] forState:UIControlStateNormal];
    } else if (count > 0) {
        [button setTitle:[NSString stringWithFormat:@"%zd", count] forState:UIControlStateNormal];
    } else {
        [button setTitle:title forState:UIControlStateNormal];
    }
}

@end
